import numpy as np
import matplotlib.pyplot as plt
from noise import snoise2  # 需要安装noise库: pip install noise

# 噪声参数设置
width, height = 100,100  # 图像尺寸
scale = 128 *0.1 # 噪声缩放系数

lacunarity = 1.025  # 间隙度
seed = 42  # 随机种子
shape = (100, 100)
octaves = 8  # 噪声八度数
persistence = 1  # 持续度
gap = 1.025  # 空隙

# 生成Perlin噪声图
world = np.zeros((height, width))
for y in range(height):
    for x in range(width):
        world[y][x] = (snoise2(
            x=x / scale,
            y=y / scale,
            octaves=octaves,
            persistence=persistence,
            lacunarity=gap,
            repeatx=1024,
            repeaty=1024,
            base=42
        ) + 1) * 0.5

# 可视化设置
plt.figure(figsize=(10, 10))
plt.imshow(world, cmap='viridis', interpolation='lanczos')
plt.colorbar(label=' 噪声强度')
plt.title(f'Perlin 噪声图\n八度数:{octaves} 缩放:{scale}', pad=20)

# 保存图片
output_path = 'perlin_noise.png'
plt.savefig(output_path, dpi=300, bbox_inches='tight')
print(f"噪声图已保存至: {output_path}")
plt.close()
